﻿using System;
using System.Collections.Generic;
using System.Linq;
using MvcEngine.Mvc.StarterSite.Controllers;
using System.Web.Mvc;
using MvcEngine.Mvc.Models.ViewModels;
using MvcEngine.Blog.Models.Entities;
using MvcEngine.Blog.Components.Extensions;
using MvcEngine.Blog.Models.ViewData;
using MvcEngine.Core;
using MvcEngine.Mvc.Extensions;
using MvcEngine.Blog.Repositories;
using MvcEngine.Core.Repositories;
using MvcEngine.Core.Helpers;
using MvcEngine.Blog.Helper;
using System.Web;
using MvcEngine.Mvc.Filters;
using MvcEngine.Core.Exceptions;
using MvcEngine.Core.Configuration;
using MvcEngine.Blog.Components.Attributes;
using MvcEngine.Mvc;
using MvcEngine.Mvc.Models;
using System.IO;
using MvcEngine.Core.Extensions;
using MvcEngine.Blog.Managers;
using MvcEngine.Core.Content;
using MvcEngine.Blog.Models;
using MvcEngine.Mvc.Controllers;
using MvcEngine.Core.Pagination;
using MvcEngine.Blog.Services;

namespace MvcEngine.Blog.BackOffice.Controllers
{
    public class BlogOverviewController : BaseController
    {
        [AcceptVerbs(HttpVerbs.Get)]
        public MvcEngineModel OverviewSettings(int ctrId)
        {
            var blogOverview = ContentManager.Current.GetPageContent(ctrId);
            Guard.ThrowIfNull(blogOverview, string.Format("Page content {0} does not exist.", ctrId));
            OverviewPageContent content = new OverviewPageContent(blogOverview);

            var allBlogs = BlogManager.Current.GetBlogs().Select(b => new BlogDataOverviewViewModel() { BlogId = b.Id, BlogName = b.Name }).ToList();

            return new BlogOverviewSettingsViewModel()
            {
                Blogs = allBlogs,
                SelectedBlogs = content.BlogIds,
                PostCount = content.PostCount
            };
        }

        [AcceptVerbs(HttpVerbs.Post)]
        [AntiForgeryValidationFilter]
        public object OverviewSettings(int ctrId, int postCount, IDictionary<Guid, bool> blogs)
        {
            if (ModelState.IsValid)
            {
                var blogOverview = ContentManager.Current.GetPageContent(ctrId);
                Guard.ThrowIfNull(blogOverview, string.Format("Page content {0} does not exist.", ctrId));

                OverviewPageContent content = new OverviewPageContent(blogOverview);
                content.PostCount = postCount;
                content.BlogIds.Clear();

                foreach (var item in blogs.Where(b => b.Value))
                {
                    var blog = BlogManager.Current.GetBlog(item.Key);
                    if (blog != null)
                    {
                        content.BlogIds.Add(blog.Id);
                    }
                }

                blogOverview.Fields = content.Fields;
                ContentManager.Current.SavePageContent(blogOverview);

                return Json(new { model = "valid" });
            }
            return Json(new { model = "invalid", errors = ModelState.GetErrors() });
        }
    }
}
